#include <iostream>
#include <cstring>
#include <unistd.h>
#include <pthread.h>
#include <stdlib.h>
using namespace std;

string ToHex(pthread_t tid)
{
    char id[64];
    snprintf(id, sizeof(id), "0x%x", tid);
    return id;
}

class ThreadReturn
{
public:
    ThreadReturn(pthread_t id, const string& info, int code)
    :id_(id)
    ,info_(info)
    ,code_(code)
    {}

public:
    pthread_t id_;
    string info_;
    int code_;
};

void *threadRoutine(void *args)
{
    string name = static_cast<const char *>(args);
    usleep(1000);
    int cnt = 5;
    while (cnt--)
    {
        cout << "new thread is running, thread name: " << name << ", my thread id: "
             << ToHex(pthread_self()) << endl;
        sleep(1);
    }
    ThreadReturn* ret = new ThreadReturn(pthread_self(),"thread quit normal", 10);
    return ret;
}

int main()
{
    pthread_t tid;
    pthread_create(&tid, nullptr, threadRoutine, (void *)"thread-1");
    cout << "I am main thread, my thread id :" << ToHex(pthread_self()) << endl;

    void *ret = nullptr;
    int n = pthread_join(tid, &ret);
    cout << "main thread done, " << " n : " << n << endl;

    ThreadReturn* r = static_cast<ThreadReturn*>(ret);    
    cout << "main thread get new thread info : " << r->code_  << ", " << ToHex(r->id_)<< ", " << r->info_<< endl;
    return 0;
}